local CommonOHOH = _G.CommonOHOH;
local M = CommonOHOH.class.new(script, CommonOHOH.files.BaseComponent());

-- 控制射箭

function M.new(self,...)
	return M(self,...);
end;


function M.ctor(self, parent)
	M.super.ctor(self, parent);

    -- 靶子射击点随机范围
   self.shootPart=self.parent.main_inst.Shoot

  local w= Instance.new("WeldConstraint",self.parent.main_inst.Shoot)
  w.Part0=self.parent.main_inst.Shoot
  self.WeldConstraint=w
   self.shootPart.CanTouch=false


   local x=self.shootPart.Size.X/2
   local y=self.shootPart.Size.Y/2
   self.Rx=x
   self.Ry=y



	self.TrainOpenEvent=self.parent.TrainOpenEvent
    self.TrainCloseEvent=self.parent.TrainCloseEvent

    --寻找攻击目标
    self.FindAttackTargetEvent=self.parent.player.FindAttackTargetEvent

    --计算伤害
    self.GetAttackDamgeEvent=self.parent.player.GetAttackDamgeEvent


    -- 训练打开
    self.TrainOpenEvent:Connect(function()

        --监听射击目标寻找
        self.FindeAttackTargetCon=  self.FindAttackTargetEvent:Connect(function(character)
              return  {pos=self:GetShootTagPos(),obj=self}
        end)

        --监听伤害计算
        self.GetAttackDamgeEventCon=  self.GetAttackDamgeEvent:Connect(function(character)
            return self.parent:GetTrianValue()
        end)


    end)

    -- 关闭射击输入检测
    self.TrainCloseEvent:Connect(function()

        if  self.FindeAttackTargetCon then
            self.FindeAttackTargetCon:Disconnect()
             self.FindeAttackTargetCon=nil
       end

       if  self.GetAttackDamgeEventCon then
            self.GetAttackDamgeEventCon:Disconnect()
            self.GetAttackDamgeEventCon=nil
        end

    end)
end;

function M.GetGuid(self)
    return  self.parent:GetGuid()
end

-- 是否射中
function M.isShootIn(self,tp)
    return tp==self.shootPart
end

-- 靶子的射击目标点
function M.GetShootTagPos(self)
    -- math.randomseed(os.clock())
    -- return self.shootPart.Position+Vector3.new(0,math.random(-self.Ry,self.Ry),math.random(-self.Rx, self.Rx))
    return self.shootPart.Position
end

local Debris=game:GetService("Debris")
--被射中
function M.PushDamge(self,damge,player,waitEvent,bullet)
    waitEvent:Connect(function()
        local rig= bullet.model.PrimaryPart:FindFirstChild("RigidConstraint")
        local att=Instance.new("Attachment",self.shootPart)
        att.WorldCFrame=bullet.model.PrimaryPart.CFrame
        rig.Attachment1=att
        bullet.model.Parent=self.shootPart
        self.parent:Hurt(damge)
        self:FlyDamge(damge)
        Debris:AddItem(att,2.5)
        return 3
    end)
end

    -- 创建Tween信息
local tweenInfo = TweenInfo.new(
    1,                -- 时间
    Enum.EasingStyle.Linear    -- 线性过渡
)

local TweenService = game:GetService("TweenService")
local TrianDamgeId="Effect.TrianDamge"
local BigNumberHelper = _G.ClientOHOH.load.BigNumberHelper();

function M.FlyDamge(self,damge)

    local effect=_G.POOL:GetObj(TrianDamgeId)
    effect.Attachment.BillboardGui.TextLabel.Text="-"..BigNumberHelper:ToString(damge)
    effect.Position=self.shootPart.Position
    local endPos=self.shootPart.Position+Vector3.new(0,5,math.random(-5, 5))
    local tween = TweenService:Create(effect, tweenInfo, {Position=endPos})
    tween.Completed:Connect(function()
        tween:Destroy()
        _G.POOL:PushObj(TrianDamgeId,effect)
    end)
    tween:Play()
end


--销毁
function M.dtor(self)

    if  self.FindeAttackTargetCon then
        self.FindeAttackTargetCon:Disconnect()
         self.FindeAttackTargetCon=nil
   end
   
    if  self.GetAttackDamgeEventCon then
        self.GetAttackDamgeEventCon:Disconnect()
        self.GetAttackDamgeEventCon=nil
    end
           
	M.super.dtor(self);
end


return M